package com.sunyard.dal.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Auther: yanj.yang
 * @Date: Create in 2020/12/11 11:10:24
 * @Description
 */
@Aspect
@Component
@Slf4j
public class MapperAspect {

    @AfterReturning("execution(* com.sunyard.dal.mapper.*Mapper.*(..))")
    public void logServiceAccess(JoinPoint joinPoint) {
    }

    @Pointcut("execution(* com.sunyard.dal.mapper.*Mapper.*(..))")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void before(JoinPoint joinPoint) throws Throwable {
        log.info("----------------SQL 执行开始----------------");
        log.info("SQL方法:{}", joinPoint.getSignature().toShortString());
        log.info("SQL参数:{}", Arrays.toString(joinPoint.getArgs()));
    }

    @Pointcut("execution(* com.sunyard.dal.mapper.*Mapper.*(..))")
    private void pointCutMethod() {
    }

    @Around("pointCutMethod()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        long begin = System.nanoTime();
        Object obj = pjp.proceed();
        long end = System.nanoTime();
        log.info("SQL耗时:{}毫秒", (end - begin) / 1000000);
        log.info("----------------SQL 执行结束----------------");
        return obj;
    }

}
